/*
* Copyright 2010 Dovado FZ-LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "AboutTableViewController.h"
#import "DovadoAppDelegate.h"
#import "LicenseViewController.h"
#import "AboutNavigationController.h"



@implementation AboutTableViewController
@synthesize generalMenuArray;
@synthesize howtoMenuArray;
@synthesize generalViewController;
@synthesize howtoRouterViewController;
@synthesize howtoClientViewController;
@synthesize licenseViewController;
@synthesize aboutNavigationController;

typedef enum {
	ABOUT_SECTION_HOWTO = 0,
	ABOUT_SECTION_GENERAL,
	ABOUT_SECTION_COUNT
} AboutSections_t;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"About";
	
	GeneralInformationViewController *general = [[GeneralInformationViewController alloc] initWithNibName:@"GeneralInformationViewController" bundle:nil];
	self.generalViewController = general;
	[general release];
	
	HowtoRouterViewController *router = [[HowtoRouterViewController alloc] initWithNibName:@"HowtoRouterViewController" bundle:nil];
	self.howtoRouterViewController = router;
	[router release];
	
	HowtoClientViewController *client = [[HowtoClientViewController alloc] initWithNibName:@"HowtoClientViewController" bundle:nil];
	self.howtoClientViewController = client;
	[client release];
	
	LicenseViewController *licensing = [[LicenseViewController alloc] initWithNibName:@"LicenseViewController" bundle:nil];
	self.licenseViewController = licensing;
	[licensing release];
	
	NSMutableArray *array = NULL;
	array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"about_menu_general",""),NSLocalizedString(@"about_menu_licensing",""), nil];
	self.generalMenuArray = array;
	[array release];
	
	array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"about_menu_router",""),NSLocalizedString(@"about_menu_client",""), nil];
	self.howtoMenuArray = array;
	[array release];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return ABOUT_SECTION_COUNT;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSInteger n = 0;
	switch (section) {
		case ABOUT_SECTION_GENERAL:
			n = [self.generalMenuArray count];
			break;
		case ABOUT_SECTION_HOWTO:
			n = [self.howtoMenuArray count];
			break;
	}
    return n;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {	
	NSString* retVal = NULL;
	switch (section) {
		case ABOUT_SECTION_GENERAL:
			retVal = NSLocalizedString(@"about_general_section", @"General");
			break;
		case ABOUT_SECTION_HOWTO:
			retVal = NSLocalizedString(@"about_howto_section", @"Howto");
			break;
		default:
			NSLog(@"Error - tried to access non-existant group!");
			retVal = 0;
			break;
	}
	return retVal;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	switch (indexPath.section) {
		case ABOUT_SECTION_GENERAL:
			cell.textLabel.text = [generalMenuArray objectAtIndex:indexPath.row];
			break;
		case ABOUT_SECTION_HOWTO:
			cell.textLabel.text = [howtoMenuArray objectAtIndex:indexPath.row];
			break;
	}
	cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	DovadoAppDelegate *appDelegate = (DovadoAppDelegate*)[[UIApplication sharedApplication] delegate];
	UIViewController *viewController = NULL;
	switch (indexPath.section) {
		case ABOUT_SECTION_GENERAL:
			switch (indexPath.row) {
				case 0:
					viewController = self.generalViewController;
					break;
				case 1:
					viewController = self.licenseViewController;
					break;
			}
			break;
		case ABOUT_SECTION_HOWTO:
			switch (indexPath.row) {
				case 0:
					viewController = self.howtoRouterViewController;
					break;
				case 1:
					viewController = self.howtoClientViewController;
					break;
			}
			break;
	}
	if(viewController != NULL) {
		[[appDelegate aboutNavigationController] pushViewController:viewController animated:YES];
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	[self.licenseViewController release];
	[super viewDidUnload];
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

